<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Admins;
use App\Models\Cates;
use App\Models\News;
use Carbon\Carbon;
use Illuminate\Http\Request;

class NewController extends Controller {

    public function index(Request $request){
        $params = [
            'kwd'  => (string)$request->get('title',''),
            'catepid'  => (int)$request->get('catepid',0),
            'column'  => (int)$request->get('column',0),
            'author_id'  => (int)$request->get('author_id',0),
            'status'  => (int)$request->get('status',0),
            'hot'  => (int)$request->get('hot',0),
            'position'  => (int)$request->get('position',0),
            'start'  => (string)$request->get('start',''),
            'end'  => (string)$request->get('end',''),
        ];
        $lists = News::where(function ($query) use ($params) {
                if($params['kwd']) $query ->where('title','like','%' . $params['kwd'] . '%');
                if($params['catepid']) $query ->where('catepid',$params['catepid']);
                if($params['status']) $query ->where('status',$params['status']);
                if($params['hot']) $query ->where('hot',$params['hot']);
                if($params['position']) $query ->where('position',$params['position']);
                if($params['start']) $query ->whereDate('created_at','>=',$params['start']);
                if($params['end']) $query ->whereDate('created_at','<=',$params['end']);
                if($params['author_id']){
                    $query ->where('author_id',$params['author_id']);
                }else{
                    if($params['column']) $query ->where('author_id','>',1);
                }
            })->orderBy('created_at', 'desc')
            ->with(['cate' => function ($query) {
                $query->select(['id','name'])->get();
            }])->with(['user' => function ($query) {
                $query->select(['id','truename'])->get();
            }])
            ->paginate(10);
        $this->stateToText($lists,[
            'status' => News::STATUS_TEXT,
            'position' => News::POSITION_TEXT,
        ]);
        return $this->writeJson(0,$lists);
    }

    //列表所需的分类筛选
    public function category(){
        $lists = Cates::where('pid',1000)->get();
        return $this->writeJson(0,$lists);
    }

    //所有正常的新闻
    public function all(){
        $lists = News::select(['id','title'])->where('status',News::STATUS_1)->orderBy('created_at', 'desc')
            ->get();
        $news = [];
        foreach($lists as $key=>$item){
            $news[$key]['key'] = $item['id'];
            $news[$key]['label'] = $item['title'];
        }
        return $this->writeJson(0,$news);
    }


    //详情
    public function info(Request $request){
        $id = (string)$request->get('id','0');
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        $info = News::findOrFail($id);
        $info['cateid'] = $info['catepid'] ? [$info['catepid'],$info['cateid']] : [$info['cateid']];
        if($info['tags']){
            $tags = [];
            foreach($info['tags'] as $item){
                $tags[] = (int)$item;
            }
            $info['tags'] = $tags;
        }
        return $this->writeJson(0,$info);
    }

    //更新
    public function update(Request $request)
    {
        $data = $request->all();
        if(count($data['cateid']) > 1){
            $data['catepid'] = $data['cateid'][0];
            $data['cateid'] = $data['cateid'][1];
        }else{
            $data['catepid'] = $data['cateid'][0];
            $data['cateid'] = 0;
        }
        if($data['id']){
            $info = News::findOrFail($data['id']);
            $info->update($data);
            return $this->writeJson(0,null,'修改新闻信息成功');
        }else{
            unset($data['id']);
            News::create($data);
            return $this->writeJson(0,null,'新增新闻信息成功');
        }
    }

    //审核通过
    public function agree(Request $request){
        $id = $request->get('id');
        if(empty($id) || !is_array($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        News::whereIn('id',$id)->update(['status'=>News::STATUS_1]);
        return $this->writeJson(0);
    }

    //审核不通过
    public function refuse(Request $request){
        $id = $request->get('id');
        if(empty($id) || !is_array($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        News::whereIn('id',$id)->update(['status'=>News::STATUS_3]);
        return $this->writeJson(0);
    }

    //删除
    public function destroy(Request $request){
        $id = $request->get('id');
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        if(is_array($id)){
            News::whereIn('id',$id)->delete();
        }else{
            News::destroy($id);
        }
        return $this->writeJson(0);
    }

}
